<?php

class Menu extends My_Controller {
	
	var $data;	// data array
	var $error=null;
    public function __construct() {
        parent::__construct("menu");
        $this->load->model('ngonngu_model');
        $this->load->model('menu_model');
        $this->load->model('utility_model');
    }

    public function index() {		
        // Build data
        $this->data['dt'] = array(
            '0' => array(
                'id' => '1',
                'title' => 'Tên menu',
                'summary' => '',
        		'content' => ''               
            )            
        );
        $this->data['ngonngu']=$this->ngonngu_model->dsNgonngu();
        // Parser data       
        $this->template->parse_view('main_content' , 'menu/edit.html' , $this->data);
        $this->template->parse_view('info_content' , 'menu/info_content_edit.html' , $this->data);			
        // Render template
        $this->template->render();
    }
    
	public function viewCreateMenu(){
	    $this->data['ngonngu']=$this->ngonngu_model->dsNgonngu();
        $this->data['menu']=$this->menu_model->dsMenu();
        if($this->error!=NULL) echo "<script>alert('".$this->error."')</script>";
		$this->template->parse_view('main_content' , 'menu/create.html' , $this->data);
        $this->template->parse_view('info_content' , 'template/info_content_create.html' , $this->data);
        // Render template
        $this->template->render();
    }
    public function doCreateMenu($id_mark=null,$id=null)
    {
        $this->load->library('form_validation');
		$this->form_validation->set_rules('tenmenu', 'Tên menu', 'xss_clean|required|min_length[5]|max_length[150]');
        if($this->form_validation->run())
        {
         $ngonngu = $this->security->xss_clean($this->input->post('ngonngu'));
         $tenmenu = $this->security->xss_clean($this->input->post('tenmenu'));
         $loaimenu = $this->security->xss_clean($this->input->post('loaimenu'));
         $checkExist=$this->utility_model->checkExist($tenmenu,'ten_menu','menu');
            if($checkExist=="false_value") 
                {
                    $this->error="Menu đã tồn tại!";
                    $this->viewCreateVideo();
                    return;
                }
            else if($checkExist=="false_field")
                {
                    $this->error="Trường không tồn tại!";
                    $this->viewCreateVideo();
                    return;
                }
        $chuthich="";
        $menucha=1;
        $thutu_menu=1;
        if($id_mark==null) $id_mark=$this->utility_model->random();
        if($this->menu_model->themMenu($tenmenu,$loaimenu,$ngonngu,$chuthich,$menucha,$thutu_menu,$id_mark,1,'admin',1))
        {
            $this->error="Thêm menu thành công";
            if($id==null) $this->viewCreateMenu(); else $this->viewEditMenu($id);    
        } else
        {
            $this->error="Xảy ra lỗi khi thêm vào CSDL!";
            if($id_mark==null) $this->viewCreateMenu(); else $this->viewEditMenu($id);
        }   
        }else 
            {
             if($id_mark==null) $this->viewCreateMenu();
                 else $this->viewEditMenu($id);   
            }
    }
    public function viewEditMenu($id)
    {
        $result=$this->menu_model->thongtinMenu($id);
        foreach($result as $row);
        $this->data['dt'] = array(
            '0' => array(
                'id' => $row['id_menu'],
                'title' => $row['ten_menu'],
                'summary' => '',
        		'content' => '',
                'id_cha'  =>$row['menu_cha']             
            )            
        );
        $this->data['id_mark']=$row['id_mark'];
        if($this->error!=NULL) echo "<script>alert('".$this->error."')</script>";
        $this->data['ngonngu']=$this->ngonngu_model->dsNgonngu();
        $this->data['menu']=$this->menu_model->dsMenu();
        // Parser data       
        $this->template->parse_view('main_content' , 'menu/edit.html' , $this->data);
        $this->template->parse_view('info_content' , 'menu/info_content_edit.html' , $this->data);			
        // Render template
        $this->template->render();
    }
    public function doEditMenu()
    {
        $this->load->library('form_validation');
        $id = $this->security->xss_clean($this->input->post('id'));
		$this->form_validation->set_rules('tenmenu', 'Tên menu', 'xss_clean|required|min_length[5]|max_length[150]');
        if($this->form_validation->run())
        {
         $ngonngu = $this->security->xss_clean($this->input->post('ngonngu'));
         $tenmenu = $this->security->xss_clean($this->input->post('tenmenu'));
         $loaimenu = $this->security->xss_clean($this->input->post('loaimenu'));
         $menucha = $this->security->xss_clean($this->input->post('menucha'));
        $chuthich="";
        $thutu_menu=1;
        if($this->menu_model->suaMenu($id,$tenmenu,$loaimenu,$ngonngu,$chuthich,$menucha,$thutu_menu,1,'admin',1))
        {
            $this->error="Update menu thành công";
            $this->viewMenus();    
        } else
        {
            $this->error="Xảy ra lỗi khi sửa dữ liệu vào CSDL!";
            $this->viewCreateMenu();
        }
        }else $this->viewEditMenu($id);
    }
    public function doDeleteMenu($id)
    {
        if($this->menu_model->xoaMenu($id))
        {
             $this->error="Xóa thành công!";
             $this->viewMenus();
        } else 
        {
            $this->error="Xảy ra lỗi!";
        }
    }
	public function viewMenus(){
		$this->data = array();
		$this->template->parse_view('main_content' , 'menu/table.html' , $this->data);
        // Render template
        $this->template->render();
    }
    public function getDataTable()
    {
			/* Indexed column (used for fast and accurate table cardinality) */
		    $sIndexColumn = "ID";
		    // DB table to use
		    $sTable = 'menu';
		    $iDisplayStart = $this->input->get_post('iDisplayStart', true);
		    $iDisplayLength = $this->input->get_post('iDisplayLength', true);
		    $iSortCol_0 = $this->input->get_post('iSortCol_0', true);
		    $iSortingCols = $this->input->get_post('iSortingCols', true);
		    $sSearch = $this->input->get_post('sSearch', true);
		    $sEcho = $this->input->get_post('sEcho', true);
		    $_aColumns=array('ten_menu','datetime_first','datetime_last','ten_nguoidung_first','trangthai_menu', 'menu_cha', 'ngonngu');
			 // Paging
		    if(isset($iDisplayStart) && $iDisplayLength != '-1' && $iDisplayLength != '0')
		    {
		        $this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
		    }
			// Ordering
		    if(isset($iSortCol_0))
		    {
		        for($i=0; $i<intval($iSortingCols); $i++)
		        {
		            $iSortCol = $this->input->get_post('iSortCol_'.$i, true);
		            $bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
		            $sSortDir = $this->input->get_post('sSortDir_'.$i, true);
		
		            if($bSortable == 'true')
		            {
		                $this->db->order_by($_aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
		            }
		        }
		    }
			if(isset($sSearch) && !empty($sSearch))
		    {
		        for($i=0; $i<count($_aColumns); $i++)
		        {
		            $bSearchable = $this->input->get_post('bSearchable_'.$i, true);
		
		            // Individual column filtering
		            if(isset($bSearchable) && $bSearchable == 'true')
		            {
		                $this->db->or_like($_aColumns[$i], $this->db->escape_like_str($sSearch));
		            }
		        }
		    }
		    // Select Data
		    $this->db->select('id_menu,ten_menu,datetime_first,datetime_last,ten_nguoidung_first,trangthai_menu,menu_cha,ngonngu.ngonngu as lang');
			$this->db->from('menu');
    	    $this->db->join('ngonngu','ngonngu.id_ngonngu = menu.ngonngu');
            $rResult = $this->db->get();
		    $this->db->select('FOUND_ROWS() AS found_rows');
		    $iFilteredTotal = $this->db->get()->row()->found_rows;
		    // Total data set length
    		$iTotal = $this->db->count_all($sTable);
		    // Output
		    $output = array(
		        'sEcho' => intval($sEcho),
		        'iTotalRecords' => $iTotal,
		        'iTotalDisplayRecords' => $iFilteredTotal,
		        'aaData' => array()
		    );
            foreach($rResult->result_array() as $hang){
                $row=array();
                $id=$hang['id_menu'];
                $linkSua=site_url("menu/viewEditMenu/$id");
                $linkXoa=site_url("menu/doDeleteMenu/$id");
                $confirm="submitLink()";
                $sua="<a href=$linkSua>Sửa</a>";
                $xoa="<a href=$linkXoa onclick='return $confirm'>Xóa</a>"; 
                $row[]=$hang['ten_menu'];
                $row[]=$hang['datetime_first'];
                $row[]=$hang['datetime_last'];
                $row[]=$hang['ten_nguoidung_first'];
                $row[]=$hang['trangthai_menu'];
                $row[]=$hang['menu_cha'];
                $row[]=$hang['lang'];
                //$row[]=$this->ngonngu_model->ngonngu($hang['ngonngu']);
                $row[]=$sua;
                $row[]=$xoa;
                $output['aaData'][]=$row;
            }
			//echo "<pre>";
			//print_r($output);
			//echo "</pre>";
			echo json_encode( $output );
    }

}